import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:xin_he/page/mine/xin_he_bank_management/xin_he_bank_entity.dart';
import 'package:xin_he/page/mine/xin_he_bank_management/xin_he_bank_list_sheet/xin_he_bank_code_entity.dart';
import 'package:xin_he/page/mine/xin_he_bank_management/xin_he_bank_management_logic.dart';
import '../../../../service/xin_he_reqest.dart';
import '../../../../utils/xin_he_event_bus.dart';
import '../../../../utils/xin_he_storage.dart';
import '../../../../utils/xin_he_toast.dart';
import 'xin_he_add_bank_state.dart';

class XinHeAddBankLogic extends GetxController {
  final XinHeAddBankState state = XinHeAddBankState();
  TextEditingController nameController = TextEditingController();
  TextEditingController bankNameController = TextEditingController();
  TextEditingController bankCardController = TextEditingController();

  XinHeBankEntity editModel = XinHeBankEntity();

  // RxMap bankMap = {}.obs;

  Rx<XinHeBankCodeEntity>  bankCodeModel = XinHeBankCodeEntity().obs;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    var model = Get.arguments;
    if(model !=null){
      editModel = model;
      state.name.value = model.name??'';
      state.bankName.value = model.bankName??'';
      state.bankCard.value = model.code??'';
      state.isDefault.value = model.isDefault == 1?true:false;
      state.id.value = model.id;



    }
    nameController     = TextEditingController(text: state.name.value);
    bankNameController = TextEditingController(text: state.bankName.value);
    bankCardController = TextEditingController(text: state.bankCard.value);
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }


  saveAddress()async {

    if(state.name.value.isEmpty){
      XinHeToast.center('持卡人不能为空');
      return;
    }
    if((bankCodeModel.value.name??'').isEmpty){
      XinHeToast.center('发卡行不能为空');
      return;
    }
    if(state.bankCard.value.length == 0){
      XinHeToast.center('银行卡号不正确');
      return;
    }

    var userId = await XinHeLocalStorage.get(XinHeStorage.userId);


    Map<String,dynamic>map = {
      // 'orderStatus':type,
      'userId': userId,
      'bankName':(bankCodeModel.value.name??'').isNotEmpty? bankCodeModel.value.name! :'',
      'code':state.bankCard.value,
      'name':state.name.value,
      'isDefault':state.isDefault.value == true?1:0,
      'name':state.name.value,
      'bankType':(bankCodeModel.value.value??'').isNotEmpty? bankCodeModel.value.value! :''

    };

    if((editModel.name??'').isNotEmpty){
      map.addAll({'id':state.id.value});
      // map.addAll({'remark':editModel.});

    }
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/bankCard/saveBankCard',params: map,isPost: true,isPostAndwwwForm: true);

    if(!result.result || result.data['code'] != 0){
      XinHeToast.center(result.data['message']??'');
      return ;
    }

    XinHeToast.center((editModel.name??'').isNotEmpty?"地址更新成功":'地址添加成功');

    bus.emit(XinHeStorage.settingOrUpdateBankInfo);

    final controllerLogic = Get.find<XinHeBankManagementLogic>();
    controllerLogic.refreshData();
    Get.back();


  }


}
